﻿Imports CustomerModel


Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Me.BindGrid()
        End If
    End Sub

    Private Sub BindGrid()
        Using entities As New CustomerEntities()
            GridView1.DataSource = From customer In entities.Customers _
                                   Select customer
            GridView1.DataBind()
        End Using
    End Sub



    Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
        Using entities As New CustomerEntities()
            Dim customer As New Customer() With { _
              .Name = txtName.Text, _
              .Country = txtCountry.Text _
            }
            entities.AddToCustomers(customer)
            entities.SaveChanges()
        End Using
        Me.BindGrid()
    End Sub

    Protected Sub OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        GridView1.EditIndex = e.NewEditIndex
        Me.BindGrid()
    End Sub


    Protected Sub OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
        Dim customerId As Integer = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Values(0))
        Dim name As String = TryCast(row.FindControl("txtName"), TextBox).Text
        Dim country As String = TryCast(row.FindControl("txtCountry"), TextBox).Text
        Using entities As New CustomerEntities()
            Dim customer As Customer = (From c In entities.Customers Where c.ID = customerId _
                                        Select c).FirstOrDefault()
            customer.Name = name
            customer.Country = country

            'my test
           
           entities.SaveChanges()
        End Using
        GridView1.EditIndex = -1
        Me.BindGrid()
    End Sub


    Protected Sub OnRowCancelingEdit(ByVal sender As Object, ByVal e As EventArgs)
        GridView1.EditIndex = -1
        Me.BindGrid()
    End Sub

    Protected Sub OnRowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        Dim customerId As Integer = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Values(0))
        Using entities As New CustomerEntities()
            Dim customer As Customer = (From c In entities.Customers Where c.ID = customerId _
                                        Select c).FirstOrDefault()
            entities.DeleteObject(customer)
            entities.SaveChanges()
        End Using
        Me.BindGrid()
    End Sub


    Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow AndAlso e.Row.RowIndex <> GridView1.EditIndex Then
            TryCast(e.Row.Cells(2).Controls(2), LinkButton).Attributes("onclick") = "return confirm('Do you want to delete this row?');"
        End If
    End Sub

End Class